import base64
import binascii

try :
    from Crypto.PublicKey import RSA
    import pubkey

    encrypting_key = RSA.importKey(pubkey.rsa_public_key)

    def encrypt(pt) :
        ptlen = len(pt)
        ptcrc = '%08x' % (binascii.crc32(pt) & 0xffffffff)
        ct = ''
        while(len(pt)>0) :
            pt_block = pt[0:127]
            pt = pt[128:]
            ct += str(encrypting_key.encrypt(pt_block,""))
        ctlen = len(ct)
        ctcrc = '%08x' % (binascii.crc32(ct) & 0xffffffff)
        ct_b64 = ct.encode("base64")
        return (ct_b64,ptlen,ptcrc,ctlen,ctcrc)
except:

    print "The Python Crypto package is not installed"
    print "The 'encrypt' function will obfuscate its input using base64"
    print "but will not perform RSA encryption."

    def encrypt(pt) :
        ptlen = len(pt)
        ptcrc = '%08x' % (binascii.crc32(pt) & 0xffffffff)
        ct = pt
        ctlen = len(ct)
        ctcrc = '%08x' % (binascii.crc32(ct) & 0xffffffff)
        ct_b64 = ct.encode("base64")
        return (ct_b64,ptlen,ptcrc,ctlen,ctcrc)
